package com.toplabs.unashu.service.impl;

import com.toplabs.unashu.dto.PageDto;
import com.toplabs.unashu.dto.SearchLog;
import com.toplabs.unashu.entity.Log;
import com.toplabs.unashu.exception.ServiceException;
import com.toplabs.unashu.mapper.LogMapper;
import com.toplabs.unashu.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ityanger
 * @since 2020-08-23
 */
@Slf4j
@Service
public class LogServiceImpl implements LogService {
    private final LogMapper logMapper;

    public LogServiceImpl(LogMapper logMapper) {
        this.logMapper = logMapper;
    }

    @Override
    public void saveLog(Log logger) {
        int insert = logMapper.insert(logger);
        if (insert == 0) {
            log.error("日志添加失败！");
        }
    }

    @Override
    public void delete(Long id) throws ServiceException {
        int delete = logMapper.delete(id);
        if (delete == 0) {
            throw new ServiceException("要删除的日志不存在");
        }
    }

    @Override
    public PageDto<Log> getLogList(SearchLog searchLog) {
        log.debug("start time: {}", searchLog.getStartTime());
        log.debug("end time: {}", searchLog.getEndTime());
        searchLog.setPageNum((searchLog.getPageNum() - 1) * searchLog.getPageSize());
        List<Log> logList = logMapper.getLogList(searchLog);
        int logTotal = logMapper.getLogTotal(searchLog);
        return new PageDto<>(logTotal, logList);
    }

    @Override
    public void batchDelete(List<Long> ids) throws ServiceException {
        for (Long id : ids) {
            int delete = logMapper.delete(id);
            if (delete == 0) {
                throw new ServiceException("要删除的日志不存在");
            }
        }
    }
}
